//
//  ShanDongTool.m
//  XinTaiPub
//
//  Created by EmberWei on 2019/1/19.
//  Copyright © 2019 rzhy. All rights reserved.
//

#import "ShanDongTool.h"
#import "NSString+Encrypt.h"

@implementation ShanDongTool

// 获取签名
+(NSString*)sign:(NSString*)module time:(NSString*)time {
    NSString *plain = [NSString stringWithFormat:@"%@%@%@%@%@",
                       ShanDong_AppSecret,
                       ShanDong_AppKey,
                       time,
                       module,
                       ShanDong_AppSecret
                       ];
    return [plain MD5];
}

// 获取参数 - 不带body
+(NSDictionary*)paramNoBody:(NSString*)module {
    // 时间戳
    NSString *rts  = [NSString rts];
    // 签名
    NSString *sign = [ShanDongTool sign:module time:rts];
    
    NSMutableDictionary *tmp = [[NSMutableDictionary alloc]init];
    [tmp setObject:ShanDong_AppKey forKey:@"appkey"];
    [tmp setObject:ShanDong_Source forKey:@"sourse"];
    [tmp setObject:module forKey:@"module"];
    [tmp setObject:rts forKey:@"time"];
    [tmp setObject:sign forKey:@"sign"];
    return (NSDictionary*)tmp;
}

// 获取参数 - 带body
+(NSDictionary*)paramWithBody:(NSString*)module
                     name:(NSString*)name
                   idCard:(NSString*)idCard
                    phone:(NSString*)phone
                      sex:(NSString*)sex {
    // 时间戳
    NSString *rts  = [NSString rts];
    // 签名
    NSString *sign = [ShanDongTool sign:module time:rts];
    // Body构建
    NSMutableDictionary *bodyMutableDict = [[NSMutableDictionary alloc]init];
    if (name) {
        [bodyMutableDict setObject:name forKey:@"name"];
    }
    if (idCard) {
        [bodyMutableDict setObject:idCard forKey:@"idCard"];
    }
    if (phone) {
        [bodyMutableDict setObject:phone forKey:@"phone"];
    }
    if (sex) {
        [bodyMutableDict setObject:sex forKey:@"sex"];
    }
    NSString *bodyString = [NSString convertToJsonString:(NSDictionary *)bodyMutableDict];
    NSString *body = [bodyString BASE64];
    
    NSMutableDictionary *tmp = [[NSMutableDictionary alloc]init];
    [tmp setObject:ShanDong_AppKey forKey:@"appkey"];
    [tmp setObject:ShanDong_Source forKey:@"sourse"];
    [tmp setObject:module forKey:@"module"];
    [tmp setObject:rts forKey:@"time"];
    [tmp setObject:sign forKey:@"sign"];
    [tmp setObject:body forKey:@"body"];
    return (NSDictionary*)tmp;
}
@end
